package client;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import msgs.*;
import msgs.TTConstants.Actions;
import java.io.*;
/**
 * Private message chat window, TigerTalk Network
 * Team 9: Andrew Hayworth, Brian Parrella, Ryan Kortmann, Nina Papa
 * @author Andrew Hayworth
 * @author Ryan Kortmann
 */

public class PMChat extends JFrame
{
	private String targetIP, targetName, fromIP;
	// this port is not changeable by the user
	private final int port = 1234;
	private Socket socket;
	
	private JPanel jpSouth;
	private JTextArea jtaContent;
	private JScrollPane jsp;
	private JTextField jtfMsg;
	private JButton jbSend;
	private JMenuBar jmb;
	private JMenu file;
	private JMenuItem sendFile;
	private File fts;
	
	private int specialResponse;
	
	/**
	 * Constructs a new PMChat window based on an incoming PMMsg with a response of TTConstants.Responses.OK
	 * @param p PMMsg to build off of
	 */
	public PMChat(PMMsg p)
	{
		this.fromIP = p.getIp();
		this.targetIP = p.getUserIP();
		this.targetName = p.getWantsUser();
		this.specialResponse = p.getSpecialResponse();
		
		setSize(400, 300);
		setLocationRelativeTo(null);
		if(p.getSpecialResponse() == 2)
		{
			setTitle("Offline message to: " + targetName);
		}
		else
		{
			setTitle("Message to: " + targetName + ": " + targetIP);
		}
		
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent we)
			{
				setVisible(false);
			}
		});
		
		jtaContent = new JTextArea();
		jtaContent.setEditable(false);
		jtaContent.setLineWrap(true);
		jtaContent.setWrapStyleWord(true);
		jsp = new JScrollPane(jtaContent);
		
		jpSouth = new JPanel(new BorderLayout());
		jtfMsg = new JTextField();
		jbSend = new JButton("Send");
		
		jpSouth.add(jtfMsg);
		jpSouth.add(jbSend, BorderLayout.EAST);
		
		jmb = new JMenuBar();
		file = new JMenu("File");
		file.setMnemonic('F');
		sendFile = new JMenuItem("Send File");
		sendFile.setMnemonic('S');
		file.add(sendFile);
		jmb.add(file);
		setJMenuBar(jmb);
		
		add(jsp);
		add(jpSouth, BorderLayout.SOUTH);
		
		jtfMsg.requestFocus();
		
		jbSend.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				// they're offline, send msgs to the server
				if(specialResponse == 2)
					{
						PMMsg p = new PMMsg(TTClient.ip,TTClient.name,TTClient.pwd, TTClient.key, targetName, jtfMsg.getText(), true);
						((ClientMsg)p).setAction(Actions.OFFLINE_MSG);
						ServerSender.outQ.add((ClientMsg) p);
					}
					else
					{
						// they're offline, send msgs to the server
						PMSender.outQ.add(new SimpleMsg(jtfMsg.getText(), TTClient.name, targetName, targetIP, fromIP, false));
					}
					
					jtaContent.append(TTClient.name + ": " + jtfMsg.getText() + "\n");
					jtfMsg.setText("");
					jtfMsg.requestFocus();
					TTClient.playMsgOut();
			}
		});
		
		jtfMsg.addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent ke)
			{
				if(ke.getKeyCode() == KeyEvent.VK_ENTER)
				{
					// they're offline, send msgs to the server
					if(specialResponse == 2)
					{
						PMMsg p = new PMMsg(TTClient.ip,TTClient.name,TTClient.pwd, TTClient.key, targetName, jtfMsg.getText(), true);
						((ClientMsg)p).setAction(Actions.OFFLINE_MSG);
						ServerSender.outQ.add((ClientMsg) p);
					}
					else
					{
						// they're offline, send msgs to the server
						PMSender.outQ.add(new SimpleMsg(jtfMsg.getText(), TTClient.name, targetName, targetIP, fromIP, false));
					}
					jtaContent.append(TTClient.name + ": " + jtfMsg.getText() + "\n");
					jtfMsg.setText("");
					jtfMsg.requestFocus();
					TTClient.playMsgOut();
				}
			}
		});
		
		// evil, ugly code follows.
		// you've been warned.
		sendFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				//complain if they're trying to send files to an offline user.
				if (specialResponse == 2) {
					JOptionPane.showMessageDialog(null, "User is offline and cannot receive files.", "User offline", JOptionPane.ERROR_MESSAGE);
					return;
				}
				// get their file
				JFileChooser fc = new JFileChooser();
				int response = fc.showOpenDialog(null);
				if (response != JFileChooser.CANCEL_OPTION) {
					FileMsg f = new FileMsg(targetIP, TTClient.ip, targetName, TTClient.name);
					f.setRequest(true);
					f.setIAnsweredTheDamnQuestionAlready(false);
					fts = fc.getSelectedFile();
					try {
						f.setFname(fts.getCanonicalPath());
					}
					catch (Exception e) {}
					// send the request over to the remote client
					PMSender.outQ.add(f);

				}
			}
		});
		
		setVisible(true);
		jtfMsg.requestFocus();
	}
	
	/////////////////
	/// Getters & Setters follow
	/////////////////
	
	/**
	 * Updates the window with the requested message
	 * @param msg Content to add to the window
	 */
	public void updateWindow(String msg) {
		jtaContent.append(targetName + ": " + msg + "\n");
	}

	public String getFromIP() {
		return fromIP;
	}

	public void setFromIP(String fromIP) {
		this.fromIP = fromIP;
	}

	/**
	 * @return the specialResponse
	 */
	public int getSpecialResponse() {
		return specialResponse;
	}

	/**
	 * @param specialResponse the specialResponse to set
	 */
	public void setSpecialResponse(int specialResponse) {
		this.specialResponse = specialResponse;
	}

	/**
	 * @return the targetIP
	 */
	public String getTargetIP() {
		return targetIP;
	}

	/**
	 * @param targetIP the targetIP to set
	 */
	public void setTargetIP(String targetIP) {
		this.targetIP = targetIP;
	}

	/**
	 * @return the targetName
	 */
	public String getTargetName() {
		return targetName;
	}

	/**
	 * @param targetName the targetName to set
	 */
	public void setTargetName(String targetName) {
		this.targetName = targetName;
	}

}